//
//  SHHScrollVC.swift
//  DoctorVisits
//
//  Created by lanou3g on 16/2/29.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class SHHScrollVC: UIViewController,UIScrollViewDelegate {

    //推荐页面的url
    let urlString = "http://api.le123.com/kuaikan/apipage_json.so?lc=863846020193282&city=CN_1_5_1&code=346e5b9d1bd97036&page=page_index&uuid=bd221acf-19f7-35c1-90bd-3ef197d4ce09&version=1.7.3&platform=Le123Plat0021&plattype=aphone"
    //轮播图详情页面url
    let detailUrlString = "http://api.le123.com/kuaikan/apisimpledetail_json.so?definition=1&lc=863846020193282&aid="
    
    //存放图片的数组
    var picArray = [String]()
    let scrollview = UIScrollView()
    let pageControl = UIPageControl()
    var timer = NSTimer()
    //屏幕的宽和高
    let width = UIScreen.mainScreen().bounds.width
    let height = UIScreen.mainScreen().bounds.height
    
    override func viewDidLoad() {
        super.viewDidLoad()

        SHHDataHelper.shareInstance.requestAllModelWithUrlString(urlString) { (array:Array<RotateModel>) -> Void in
            print("呵呵")
            print(array.count)
            for rotate in array {
//                print(rotate.pic!)
                self.picArray.append(rotate.pic!)
            }
            print(self.picArray[0])
            
            self.scrollview.frame = CGRectMake(0, 0, self.width, self.height*0.3)
            //循环图片，添加到scollview上
            for var i=0; i<self.picArray.count; i++ {
                let imageVC = UIImageView()
//                let imageX = CGFloat(i*self.width)
                //此处i要强转成CGFloat类型
                imageVC.frame = CGRectMake(CGFloat(i)*CGRectGetWidth(self.scrollview.frame), 0, CGRectGetWidth(self.scrollview.frame), CGRectGetHeight(self.scrollview.frame))
                imageVC.sd_setImageWithURL(NSURL(string: self.picArray[i]), placeholderImage: UIImage(named: "picholder"))
                self.scrollview.showsHorizontalScrollIndicator = false
                self.scrollview.addSubview(imageVC)
            }
            
            //设置样式
            self.scrollview.contentSize = CGSizeMake(CGFloat(self.picArray.count) * CGRectGetWidth(self.scrollview.frame), 0)
            self.scrollview.pagingEnabled = true
            self.scrollview.delegate = self
            self.view.addSubview(self.scrollview)
            //设置pageControl
            self.pageControl.frame = CGRectMake(0, self.height*0.3-20, self.width, 20)
            self.pageControl.currentPage = 0
            self.pageControl.currentPageIndicatorTintColor = UIColor.whiteColor()
            self.pageControl.pageIndicatorTintColor = UIColor.blackColor()
            self.pageControl.numberOfPages = self.picArray.count
            self.view.addSubview(self.pageControl)
            self.addTimer()
        }
        
    }
    
    
    //scrollview的代理方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let scrollviewW =  scrollView.frame.size.width
        let page = Int((scrollview.contentOffset.x + CGFloat( scrollviewW / 2)) / scrollviewW)
        self.pageControl.currentPage = page
    }
    
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        self.removeTimer()
    }
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.addTimer()
    }
    
    //定时器
    func addTimer()->Void{
        self.timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "nextImage", userInfo: nil, repeats: true)
    }
    
    //关闭定时器
    func removeTimer(){
        self.timer.invalidate()
    }
    
    func nextImage(){
        var page = Int(self.pageControl.currentPage)
        //println(self.picArray.count)
        if (page == self.picArray.count-1){
            page = 0
        }else{
            page = page+1
        }
        self.scrollview.contentOffset = CGPointMake(CGFloat(page)*self.scrollview.frame.size.width, 0)
    }
    
    //轮播图的创建
    func loadScrollView()->Void{
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
